package uestc.lj.leetcode.MT;

import java.util.Scanner;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * @Author:Crazlee
 * @Date:2021/12/25
 */
public class meituan002 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		scanner.nextLine();
		int[] weigth = new int[n];
		for (int i = 0; i < n; i++) {
			weigth[i] = scanner.nextInt();
		}
		int[] number = new int[n];
		for (int i = 0; i < n; i++) {
			number[i] = scanner.nextInt();
		}
		int[] prefixSum = new int[n + 1];
		for (int i = 0; i < n; i++) {
			prefixSum[i + 1] = weigth[i] + prefixSum[i];
		}
		int[] ans = new int[n];
		TreeSet<Integer> bound = new TreeSet<>();
		bound.add(0);
		bound.add(n + 1);
		TreeMap<Integer, Integer> sumMap = new TreeMap<>();

		for (int i = 0; i < n; i++) {
			int pos = number[i];
			int left = bound.lower(pos);
			int right = bound.higher(pos);
			bound.add(pos);

			int segSum = prefixSum[right - 1] - prefixSum[left];
			Integer count = sumMap.get(segSum);
			if (count != null) {
				if (count == 1) {
					sumMap.remove(segSum);
				} else {
					sumMap.put(segSum, count - 1);
				}
			}

			int leftSum = prefixSum[pos - 1] - prefixSum[left];
			int rightSum = prefixSum[right - 1] - prefixSum[pos];
			sumMap.put(leftSum, sumMap.getOrDefault(leftSum, 0) + 1);
			sumMap.put(rightSum, sumMap.getOrDefault(rightSum, 0) + 1);
			ans[i] = sumMap.lastKey();
		}
		for (int i = 0; i < n; i++) {
			System.out.println(ans[i]);
		}
	}
}
